package com.daikuan.lizh.ui.fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.daikuan.lizh.adapter.HomeGridViewAdapter;
import com.daikuan.lizh.adapter.HomeListAdapter2;
import com.daikuan.lizh.bmobBean.ButtonConfigBean;
import com.daikuan.lizh.ui.activity.YouXuanActivity;
import com.daikuan.lizh.widget.MyGridView;
import com.jarhead.common.base.BaseFragment;
import com.jarhead.common.commonutils.ToastHelper;
import com.lcodecore.tkrefreshlayout.Footer.LoadingView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import com.daikuan.lizh.R;
import com.daikuan.lizh.bmobBean.ListConfig;
import com.daikuan.lizh.ui.activity.WebViewActivity;
import com.daikuan.lizh.widget.ScrollViewWithListView;

import org.json.JSONArray;

import java.util.List;

import butterknife.Bind;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by jiming1 on 2017/6/30.
 */
public class HomeFragment extends BaseFragment {


	@Bind(R.id.listView)
	ScrollViewWithListView scrollViewWithListView;
	@Bind(R.id.refresh)
	TwinklingRefreshLayout refreshLayout;
	@Bind(R.id.gridView_8)
	MyGridView gridView_8;

	HomeGridViewAdapter homeGridViewAdapter;
	LayoutInflater layoutInflater;
	List<ListConfig> listConfigs;
	List<ButtonConfigBean> buttonConfigBeens;
	HomeListAdapter2 adapter;
	int count = 0;

	@Override
	protected int getLayoutResource() {
		return R.layout.fragment_home;
	}

	@Override
	public void initPresenter() {

	}

	@Override
	protected void initView() {
		layoutInflater = LayoutInflater.from(getActivity());
		SinaRefreshView headerView = new SinaRefreshView(getActivity());
		headerView.setArrowResource(R.mipmap.arrow);
		headerView.setTextColor(0xff745D5C);
		refreshLayout.setHeaderView(headerView);
		LoadingView loadingView = new LoadingView(getActivity());
		refreshLayout.setBottomView(loadingView);
		refreshLayout.setEnableLoadmore(true);


		gridView_8.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ButtonConfigBean buttonConfigBean = homeGridViewAdapter.getItem(position);
				if (buttonConfigBean == null)
					return;
				YouXuanActivity.jump(buttonConfigBean.type_id, buttonConfigBean.type_name, getActivity());

			}
		});

		refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
			@Override
			public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
				count = 0;
				getButtonConfig();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						refreshLayout.finishRefreshing();
					}
				}, 2000);
			}

			@Override
			public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
				super.onLoadMore(refreshLayout);
				count++;
				getList(true);
			}
		});

		scrollViewWithListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				ListConfig listConfig = adapter.getItem(i);
				WebViewActivity.skip(getActivity(), listConfig.channel_gourl, listConfig.channel_name);
			}
		});
	}

	@Override
	protected void initData() {
		getList(false);
		getButtonConfig();
	}


	private void getList(final boolean isLoadMore) {

		BmobQuery query = new BmobQuery("list_config");
		query.order("createdAt");
		query.setSkip(count * 10);
		query.setLimit(10);
		query.findObjectsByTable(new QueryListener<JSONArray>() {
			@Override
			public void done(JSONArray ary, BmobException e) {
				if (refreshLayout != null)
					refreshLayout.finishLoadmore();
				try {
					if (e == null) {
						String str = ary.toString();
						Log.e("XXX-list", str);
						if (isLoadMore) {
							if (JSON.parseArray(ary.toString(), ListConfig.class).size() == 0) {
								ToastHelper.toast("没有更多了");
								return;
							}
							adapter.add(JSON.parseArray(ary.toString(), ListConfig.class));
						} else {

							listConfigs = JSON.parseArray(ary.toString(), ListConfig.class);
							if (adapter == null) {
								adapter = new HomeListAdapter2(getActivity(), listConfigs);
								scrollViewWithListView.setAdapter(adapter);
							} else {
								adapter.update(listConfigs);
							}
						}

					} else {
						ToastHelper.toast("获取数据失败");
					}
				} catch (Exception e2) {
					ToastHelper.toast("获取数据失败");
				}
			}
		});

	}

	private void getButtonConfig() {

		BmobQuery query = new BmobQuery("type_info");
		query.order("createdAt");
		query.findObjectsByTable(new QueryListener<JSONArray>() {
			@Override
			public void done(JSONArray ary, BmobException e) {
				if (refreshLayout != null)
					refreshLayout.finishLoadmore();
				try {
					if (e == null) {
						String str = ary.toString();
						Log.e("XXX-button", str);
						buttonConfigBeens = JSON.parseArray(ary.toString(), ButtonConfigBean.class);
						if (homeGridViewAdapter == null) {
							homeGridViewAdapter = new HomeGridViewAdapter(getActivity(), buttonConfigBeens);
							gridView_8.setAdapter(homeGridViewAdapter);
						} else {
							homeGridViewAdapter.notifyDataSetChanged();
						}

					} else {
						ToastHelper.toast("获取数据失败");
					}
				} catch (Exception e2) {
					ToastHelper.toast("获取数据失败");
				}
			}
		});

	}
}
